/**
 * Copyright 2013-2022 the original author or authors from the Jeddict project (https://jeddict.github.io/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package io.github.jeddict.jpa.modeler.properties.order;

import java.awt.CardLayout;
import io.github.jeddict.jpa.modeler.widget.attribute.AttributeWidget;
import static io.github.jeddict.jpa.modeler.properties.PropertiesHandler.NONE_TYPE;
import io.github.jeddict.jpa.modeler.properties.order.by.OrderByPanel;
import io.github.jeddict.jpa.modeler.properties.order.column.OrderColumnPanel;
import io.github.jeddict.jpa.spec.OrderBy;
import io.github.jeddict.jpa.spec.OrderColumn;
import io.github.jeddict.jpa.spec.extend.SortableAttribute;
import org.netbeans.modeler.properties.embedded.GenericEmbeddedEditor;

/**
 *
 * @author Gaurav Gupta
 */
public class OrderPanel extends GenericEmbeddedEditor<SortableAttribute> {

    private final AttributeWidget rootAttributeWidget;
    private SortableAttribute sortableAttribute;
    public final static String ORDER_BY = "OrderBy", ORDER_COLUMN = "OrderColumn";
    private OrderColumn orderColumn;
    private OrderBy orderBy;

    public OrderPanel(AttributeWidget attributeWidget) {
        this.rootAttributeWidget = attributeWidget;
    }

    @Override
    public void init() {
        initComponents();
    }

    @Override
    public SortableAttribute getValue() {
        String actionType = (String) orderTypeComboBox.getSelectedItem();
        if (ORDER_BY.equals(actionType)) {
            sortableAttribute.setOrderBy(getOrderByPanel().getValue());
            sortableAttribute.setOrderColumn(null);
        } else if (ORDER_COLUMN.equals(actionType)) {
            sortableAttribute.setOrderColumn(getOrderColumnPanel().getValue());
            sortableAttribute.setOrderBy(null);
        } else {
            sortableAttribute.setOrderBy(null);
            sortableAttribute.setOrderColumn(null);
        }
        return sortableAttribute;
    }

    @Override
    public void setValue(SortableAttribute sortableAttribute) {
        this.sortableAttribute = sortableAttribute;
        this.orderBy = sortableAttribute.getOrderBy();
        this.orderColumn = sortableAttribute.getOrderColumn();
        if (orderColumn == null) {
            orderColumn = new OrderColumn();
        }
        if (orderBy == null) {
            orderBy = new OrderBy();
        }
        orderTypeComboBox.setSelectedItem(getState(sortableAttribute));
    }

    public static String getState(SortableAttribute sortableAttribute) {
        return sortableAttribute.getOrderBy() != null ? ORDER_BY : (sortableAttribute.getOrderColumn() != null ? ORDER_COLUMN : NONE_TYPE);
    }
    
    public static String getStateDisplay(SortableAttribute sortableAttribute) {
        return sortableAttribute.getOrderBy() != null ? ORDER_BY + String.format("[%s]",sortableAttribute.getOrderBy().getAttributes().size()) : 
                (sortableAttribute.getOrderColumn() != null ? ORDER_COLUMN + String.format("[%s]",sortableAttribute.getOrderColumn().getName()) :
                NONE_TYPE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        main_LayeredPane = new javax.swing.JLayeredPane();
        orderTypeWrapper = new javax.swing.JPanel();
        orderTypeComboBox = new javax.swing.JComboBox<>();
        orderTypePanel = new javax.swing.JPanel();
        none = new javax.swing.JLayeredPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();
        orderByPanel = new OrderByPanel(rootAttributeWidget);
        orderColumnPanel = new OrderColumnPanel();

        main_LayeredPane.setLayout(new java.awt.BorderLayout());

        orderTypeWrapper.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        orderTypeWrapper.setPreferredSize(new java.awt.Dimension(56, 35));
        orderTypeWrapper.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        orderTypeComboBox.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "< None >", "OrderBy", "OrderColumn" }));
        orderTypeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                orderTypeComboBoxActionPerformed(evt);
            }
        });
        orderTypeWrapper.add(orderTypeComboBox);

        main_LayeredPane.add(orderTypeWrapper, java.awt.BorderLayout.PAGE_START);

        orderTypePanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        orderTypePanel.setLayout(new java.awt.CardLayout());

        none.setPreferredSize(new java.awt.Dimension(570, 200));

        jEditorPane1.setBackground(new java.awt.Color(244, 244, 244));
        jEditorPane1.setContentType("text/html"); // NOI18N
        jEditorPane1.setForeground(new java.awt.Color(102, 102, 102));
        jEditorPane1.setText(org.openide.util.NbBundle.getMessage(OrderPanel.class, "Order.document.info")); // NOI18N
        jScrollPane2.setViewportView(jEditorPane1);

        none.setLayer(jScrollPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout noneLayout = new javax.swing.GroupLayout(none);
        none.setLayout(noneLayout);
        noneLayout.setHorizontalGroup(
            noneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE)
        );
        noneLayout.setVerticalGroup(
            noneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(noneLayout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 344, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        orderTypePanel.add(none, "< None >");

        orderByPanel.setPreferredSize(new java.awt.Dimension(570, 400));

        javax.swing.GroupLayout orderByPanelLayout = new javax.swing.GroupLayout(orderByPanel);
        orderByPanel.setLayout(orderByPanelLayout);
        orderByPanelLayout.setHorizontalGroup(
            orderByPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 497, Short.MAX_VALUE)
        );
        orderByPanelLayout.setVerticalGroup(
            orderByPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 344, Short.MAX_VALUE)
        );

        orderTypePanel.add(orderByPanel, "OrderBy");

        orderColumnPanel.setPreferredSize(new java.awt.Dimension(570, 200));

        javax.swing.GroupLayout orderColumnPanelLayout = new javax.swing.GroupLayout(orderColumnPanel);
        orderColumnPanel.setLayout(orderColumnPanelLayout);
        orderColumnPanelLayout.setHorizontalGroup(
            orderColumnPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 497, Short.MAX_VALUE)
        );
        orderColumnPanelLayout.setVerticalGroup(
            orderColumnPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 344, Short.MAX_VALUE)
        );

        orderTypePanel.add(orderColumnPanel, "OrderColumn");

        main_LayeredPane.add(orderTypePanel, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(main_LayeredPane, javax.swing.GroupLayout.DEFAULT_SIZE, 501, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(main_LayeredPane, javax.swing.GroupLayout.PREFERRED_SIZE, 383, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void orderTypeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_orderTypeComboBoxActionPerformed
        String actionType = (String) orderTypeComboBox.getSelectedItem();
        if (ORDER_BY.equals(actionType)) {
            if (!getOrderByPanel().isLoaded()) {
                getOrderByPanel().init();
            }
            getOrderByPanel().setValue(orderBy);
        } else if (ORDER_COLUMN.equals(actionType)) {
            if (!getOrderColumnPanel().isLoaded()) {
                getOrderColumnPanel().init();
            }
            getOrderColumnPanel().setValue(orderColumn);
        }
        CardLayout orderTypeLayout = (CardLayout) orderTypePanel.getLayout();
        orderTypeLayout.show(orderTypePanel, (String) orderTypeComboBox.getSelectedItem());
    }//GEN-LAST:event_orderTypeComboBoxActionPerformed

    private OrderByPanel getOrderByPanel() {
        return (OrderByPanel) orderByPanel;
    }

    private OrderColumnPanel getOrderColumnPanel() {
        return (OrderColumnPanel) orderColumnPanel;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLayeredPane main_LayeredPane;
    private javax.swing.JLayeredPane none;
    private javax.swing.JPanel orderByPanel;
    private javax.swing.JPanel orderColumnPanel;
    private javax.swing.JComboBox<String> orderTypeComboBox;
    private javax.swing.JPanel orderTypePanel;
    private javax.swing.JPanel orderTypeWrapper;
    // End of variables declaration//GEN-END:variables
}
